package com.edu.lxshpc.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.lxshpc.pojo.Blog;
import com.edu.lxshpc.pojo.BlogCategory;
import com.edu.lxshpc.mapper.BlogMapper;
import com.edu.lxshpc.service.BlogCategoryService;
import com.edu.lxshpc.mapper.BlogCategoryMapper;
import com.edu.lxshpc.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.edu.lxshpc.utils.RedisStatic.*;

/**
* @author Sanshan
* @description 针对表【blog_category】的数据库操作Service实现
* @createDate 2023-11-09 10:05:42
*/
@Service
@Transactional
public class BlogCategoryServiceImpl extends ServiceImpl<BlogCategoryMapper, BlogCategory>
    implements BlogCategoryService{

    @Autowired
    private BlogCategoryMapper blogCategoryMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private BlogMapper blogMapper;
    /**
     * 获得文章分类列表
     * @return 文章分类列表
     */
    @Override
    public Result getBlogCategoryList() {
        // redis缓存，缓存不存在，查数据库
        if(Boolean.TRUE.equals(stringRedisTemplate.hasKey(BLOG_CATEGORY_KEY))){
            String jsonStr = stringRedisTemplate.opsForValue().get(BLOG_CATEGORY_KEY);
            List<BlogCategory> blogCategoryList = JSONUtil.toList(JSONUtil.parseArray(jsonStr), BlogCategory.class);
            return Result.success(blogCategoryList);
        }
        // 从数据库查询
        List<BlogCategory> blogCategoryList = blogCategoryMapper.selectList(new QueryWrapper<BlogCategory>().orderByAsc("category_id"));
        String jsonStr = JSONUtil.toJsonStr(blogCategoryList);
        stringRedisTemplate.opsForValue().set(BLOG_CATEGORY_KEY,jsonStr,EXPIRE, TimeUnit.MINUTES);
        return Result.success(blogCategoryList);
    }

    /**
     * 风险提示：
     * 该方法用于获取指定类别的博客列表
     * @param id 类别id
     * @return 博客列表结果
     */
    @Override
    public Result getBlogListByCategory(Integer id) {
        String key = BLOG_CATEGORY_CATEGORYID_KEY + id;
        // redis缓存，缓存不存在，查数据库
        if(Boolean.TRUE.equals(stringRedisTemplate.hasKey(key))){
            String jsonStr = stringRedisTemplate.opsForValue().get(key);
            List<Blog> blogCategoryList = JSONUtil.toList(JSONUtil.parseArray(jsonStr), Blog.class);
            return Result.success(blogCategoryList);
        }
        // 从数据库查询
        List<Blog> blogList = blogMapper.selectList(new QueryWrapper<Blog>().eq("category_id",id).orderByDesc("create_time"));
        String jsonStr = JSONUtil.toJsonStr(blogList);
        stringRedisTemplate.opsForValue().set(key,jsonStr,EXPIRE, TimeUnit.MINUTES);
        return Result.success(blogList);
    }
}




